var strtotime = function (timestr) {
	var timearr = timestr.split('-');
	var oDate = new Date((timearr[0]).toInt(), (timearr[1]).toInt(), (timearr[2]).toInt());
	return oDate.getTime();
}

var validate = function (form) {
	var fobj = $(form);
	if (fobj) {
		if ('' == fobj.title.value) {
			alert('Task title can not be empty!');
			return ;
		}
		if (fobj.date_start && fobj.date_end) {
			if (strtotime(fobj.date_start.value) > strtotime(fobj.date_end.value)) {
				alert('Start date should be earlier than the end date!');
				return;
			}
		}
		if (fobj.actual_date_start && fobj.actual_date_end) {
			if (strtotime(fobj.actual_date_start.value) > strtotime(fobj.actual_date_end.value)) {
				alert('Actual start date should be earlier than the end date!');
				return;
			}
		}
		if (!countWeight(form)) return ;
		if ($('savingbox')) {
			$('savingbox').setStyle('display', 'block');
		}
		fobj.save.value = 1;
		fobj.submit();
	}
	return false;
}

var countWeight = function (form) {
	var sum_weight = 0;
	var has_weight = false;
	$(form).getElements('input[name^=weight]').each(function(item, index){
		sum_weight += parseInt(item.value);
		has_weight = true;
	});
	if (has_weight && sum_weight != 100) {
		if ($('weight_notice')) $('weight_notice').setHTML('<font style="color:red;font-weight:bold">* Sum of the sub tasks weight must equal to 100 !</font>');
		return false;
	}
	if ($('weight_notice'))$('weight_notice').setHTML('<font style="color:black;font-weight:bold">* You can change the weight of the subtasks above !</font>');
	return true;
}

var getWeight = function (form) {
	var weight = {};
	$(form).getElements('input[name^=weight]').each(function(item, index){
		eval("weight."+$(item).name+" = $(item).value");
	});
	return Json.toString(weight);
}

var loadopen = function (url) {
	if ($('loadingbox')) {
		$('loadingbox').setStyle('display', 'block');
	}
	if (url.length > 0) redirect(url);
}

var config_validate = function (form) {
	var fobj = $(form);
	if (fobj) {
		var levelcolor = '';
		$ES('li','levelcolor').each(function(item, index){
			var color = item.getStyle('background-color');
			if (levelcolor != '') {
				levelcolor += ','+color;
			} else {
				levelcolor += color;
			}
		});
		fobj.level.value = levelcolor;
		fobj.save.value = 1;
		fobj.submit();
	}
}

var showTaskUpdateDetail = function(a,id) {
	var id = 'stu-'+id;
	if ($(id).style.display != 'block') {
		$(a).innerHTML = '<img src="/images/collapse.gif">';
		$(id).style.display = 'block';
	} else {
		$(a).innerHTML = '<img src="/images/expand.gif">';
		$(id).style.display = 'none';
	} 
}